import json

import aiomysql
from re_common.baselibrary.database.mysql import Mysql
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.core.mdeprecated import retry_func_async, try_except2_async
from re_common.baselibrary.utils.core.requests_core import INSIDE_HEADERS

from apps.crawler_platform.core_api.models import UpdateSqlModel, AddManyModel, UpdateSqlListModel

from apps.crawler_platform.core_platform.core_g import CoreSqlValue, SQLTable
from apps.crawler_platform.core_platform.g_model import InputPlatformModel
from apps.crawler_platform.util.requestapihelper import RequestApiHelper
from apps.crawler_platform.util.sqlhelper import SQLHelper
from settings import URLDISTRIBUTED

"""
过api执行的操作
"""


class CoreSqlMixin(object):
    # 循环导入问题，在类的内部导入
    from apps.crawler_platform.core_callback.err_callback import all_client_back_callback
    @staticmethod
    @retry_func_async(retry_times=3, sleep_time=2)
    @try_except2_async(all_client_back_callback, is_print=True)
    async def update_sql(table, update_dict, update_no_placeholder, where_dict):

        assert table, 'update_sql table不能为空'
        url = await URLDISTRIBUTED.get_sql_update_url()
        data = InputPlatformModel[UpdateSqlModel](
            data=UpdateSqlModel(
                table=table,
                update=update_dict,
                update_no_placeholder=update_no_placeholder,
                where=where_dict
            )
        ).json()
        rrq = RequestApiHelper.get_rrq()
        kwargs = {
            "rrq": rrq,
            "header": INSIDE_HEADERS,
            "url": url,
            "timeout": 30,
            "data": data,
            "moths": MRequest.POST,
            "middler_list": [rrq.status_code_middlerwares, rrq.is_none_html_middlerwares,
                             rrq.is_null_html_middlerwares, rrq.msg_status_code_middlerwares]
        }
        bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
        return bools, dicts

    @staticmethod
    @retry_func_async(retry_times=3, sleep_time=2)
    @try_except2_async(is_print=True)
    async def update_list_sql(lists):
        url = await URLDISTRIBUTED.get_sql_update_list_url()
        list_model = []
        for item in lists:
            table, update_dict, update_no_placeholder, where_dict = item
            m = UpdateSqlModel(
                table=table,
                update=update_dict,
                update_no_placeholder=update_no_placeholder,
                where=where_dict
            )
            list_model.append(m)
        data = InputPlatformModel[UpdateSqlListModel](
            data=UpdateSqlListModel(
                data_list=list_model
            )
        ).json()
        rrq = RequestApiHelper.get_rrq()
        kwargs = {
            "rrq": rrq,
            "header": INSIDE_HEADERS,
            "url": url,
            "timeout": 100,  # 因为可能有几百的list更新，需要更加长的时间
            "data": data,
            "moths": MRequest.POST,
            "middler_list": [rrq.status_code_middlerwares, rrq.is_none_html_middlerwares,
                             rrq.is_null_html_middlerwares, rrq.msg_status_code_middlerwares]
        }
        bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
        return bools, dicts

    @staticmethod
    @retry_func_async(retry_times=3, sleep_time=2)
    @try_except2_async(is_print=True)
    async def insert_many_sql(table, feild_list, value_list, insert=CoreSqlValue.insert_ig_it):
        url = await URLDISTRIBUTED.get_sql_add_many_url()
        data = InputPlatformModel[AddManyModel](
            data=AddManyModel(
                table=table,
                feild_list=feild_list,
                value_list=value_list,
                insert=insert,
            )
        ).json()
        rrq = RequestApiHelper.get_rrq()
        timeout = 30
        if len(value_list) > 1000:
            timeout = len(value_list)
        kwargs = {
            "rrq": rrq,
            "header": INSIDE_HEADERS,
            "url": url,
            "timeout": timeout,
            "data": data,
            "moths": MRequest.POST,
            "middler_list": [rrq.status_code_middlerwares, rrq.is_none_html_middlerwares, rrq.is_null_html_middlerwares,
                             rrq.msg_status_code_middlerwares]
        }
        bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
        if bools:
            dicts = json.loads(rrq.html)
        return bools, dicts

    @staticmethod
    async def get_homelist(task_name, task_tag, home_rawid, page_index):
        sql = f"select * from {SQLTable.journal_home} where task_name=%s and task_tag=%s and home_rawid=%s and page_index=%s"
        args = (task_name, task_tag, home_rawid, page_index)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_other_homelist(task_name, task_tag, list_rawid, page_index):
        sql = f"select * from {SQLTable.other_home} where task_name=%s and task_tag=%s and list_rawid=%s and page_index=%s"
        args = (task_name, task_tag, list_rawid, page_index)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    # 不通过api执行的操作,量小 比较有保证且不需要分布式分担压力
    @staticmethod
    async def get_journallist(task_name, task_tag, journal_rawid):
        sql = f"select * from {SQLTable.journal_list} where task_name=%s and task_tag=%s and journal_rawid=%s"
        args = (task_name, task_tag, journal_rawid)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_export_journallist(task_name, task_tag):
        # from main import app
        # sql = f"select * from {SQLTable.journal_list} where state=1 and task_name=%s and task_tag=%s"
        if task_name == "cnkijournal":
            sql = f"select * from {SQLTable.journal_list} where is_active = 2 and state in (-1,1,10) and task_name=%s and task_tag=%s"
        else:
            sql = f"select * from {SQLTable.journal_list} where state = 1 and task_name=%s and task_tag=%s"
        args = (task_name, task_tag)
        rows = await SQLHelper.fetchall(sql, args, aiomysql.DictCursor)
        return rows

    @staticmethod
    async def get_code_publisher():
        # from main import app
        sql = f"select * from {SQLTable.code_publisher}"
        args = None
        rows = await SQLHelper.fetchall(sql, args, aiomysql.DictCursor)
        return rows

    @staticmethod
    async def get_volumelist(task_name, task_tag, journal_rawid, pub_year, num):
        sql = f"select * from {SQLTable.journal_volume} where task_name=%s and task_tag=%s and journal_rawid=%s and pub_year=%s and num=%s"
        args = (task_name, task_tag, journal_rawid, pub_year, num)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_issuelist(task_name, task_tag, journal_rawid, pub_year, num, page_index):
        sql = f"select * from {SQLTable.journal_issue} where task_name=%s and task_tag=%s and journal_rawid=%s and pub_year=%s and num=%s and page_index=%s"
        args = (task_name, task_tag, journal_rawid, pub_year, num, page_index)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_article(task_name, task_tag, rawid):
        sql = f"select * from {SQLTable.journal_article} where task_name=%s and task_tag=%s and rawid=%s"
        args = (task_name, task_tag, rawid)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_qk_etl_local_article(table, index_name, task_name, task_tag, rawid):
        sql = f"select * from {table} where task_name=%s and task_tag=%s and rawid=%s"
        args = (task_name, task_tag, rawid)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        if row:
            article_info_json = row.get("article_info_json", "")
            if len(article_info_json) > 0:
                jjson = json.loads(article_info_json)
                journal_rawid = jjson.get("journal_rawid", "")
                if len(journal_rawid) > 0:
                    journal_sql = f"select * from {SQLTable.journal_list} where task_name=%s and journal_rawid=%s"
                    args2 = (task_name, journal_rawid)
                    j_row = await SQLHelper.fetchone(journal_sql, args2, aiomysql.DictCursor)
                    row["journal_info"] = json.loads(j_row["journal_json"])
        return row

    @staticmethod
    async def get_sql_etl_parse(table, index_name, task_name, task_tag, id_dicts):
        """
        查询sql数据
        :param table: 表名
        :param index_name: 索引名
        :param task_name: task_name
        :param task_tag: task_tag
        :param id_dicts: 主id可能由多个组成
        :return: 返回查询到的sql数据
        """
        lists_v = [task_name, task_tag]
        list_k = []
        for k, v in id_dicts.items():
            lists_v.append(v)
            list_k.append(k)
        strs = ""
        for k in list_k:
            strs = strs + f" and {k} = %s"
        if index_name:
            sql = f"select * from {table} where task_name=%s and task_tag=%s" + strs
        else:
            sql = f"select * from {table} where task_name=%s and task_tag=%s" + strs
        args = tuple(lists_v)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_sql_etl_parse_journal_info(task_name, journal_rawid):
        journal_sql = f"select * from {SQLTable.journal_list} where task_name=%s and journal_rawid=%s"
        args2 = (task_name, journal_rawid)
        j_row = await SQLHelper.fetchone(journal_sql, args2, aiomysql.DictCursor)
        return j_row

    @staticmethod
    async def get_sql_etl_parse_other_journal_info(task_name, task_tag, list_rawid):
        journal_sql = f"select * from {SQLTable.other_list}  where task_name=%s and list_rawid=%s"
        args2 = (task_name, list_rawid)
        j_row = await SQLHelper.fetchone(journal_sql, args2, aiomysql.DictCursor)
        return j_row

    @staticmethod
    async def get_etl_remote_article(table, index_name, task_name, task_tag, rawid):
        in_str = ",".join(["%s" for i in range(0, len(rawid))])
        # in_str = ""
        # for item in rawid:
        #     in_str += "'{}',".format(Mysql.escape(item))
        # in_str = in_str[0:-1]
        if index_name:
            sql = f"select * from {table} where task_name=%s and task_tag=%s and rawid in (" + in_str + ")"
        else:
            sql = f"select * from {table} where task_name=%s and task_tag=%s and rawid in (" + in_str + ")"
        # sql = f"-- select * from {SQLTable.journal_article} force index(m_rawid)  where task_name='cnkijournal' and task_tag='cnkinet1starticle' and rawid in (" + in_str + ")"
        args = (task_name, task_tag) + tuple(rawid)
        rows = await SQLHelper.fetchall(sql, args, aiomysql.DictCursor)
        return rows

    @staticmethod
    async def get_etl_ref_remote_article(table, index_name, task_name, task_tag, rawid):
        in_str = ",".join(["%s" for i in range(0, len(rawid))])
        sql = f"select * from {table} where task_name=%s and task_tag=%s and rawid in (" + in_str + ")"
        args = (task_name, task_tag) + tuple(rawid)
        rows = await SQLHelper.fetchall(sql, args, aiomysql.DictCursor)
        return rows

    @staticmethod
    async def update_etl_local_article(table, index_name, stname, task_name, task_tag, rawid, code, err_msg):
        if index_name:
            sql = f"update {table} set {stname}=%s,err_msg=%s where task_name=%s and task_tag=%s and rawid =%s"
        else:
            sql = f"update {table} set {stname}=%s,err_msg=%s where task_name=%s and task_tag=%s and rawid =%s"
        args = (code, err_msg, task_name, task_tag, rawid)
        await SQLHelper.execute_commit(sql, args)

    @staticmethod
    async def update_etl_ref_local_article(table, index_name, stname, task_name, task_tag, rawid, code, err_msg):
        sql = f"update {table} set {stname}=%s,err_msg=%s where task_name=%s and task_tag=%s and rawid =%s"
        args = (code, err_msg, task_name, task_tag, rawid)
        await SQLHelper.execute_commit(sql, args)

    @staticmethod
    async def update_etl_remote_article(table, index_name, stname, task_name, task_tag, list_rawid, code, err_msg):
        in_str = ",".join(["%s" for i in range(0, len(list_rawid))])
        if index_name:
            sql = f"update {table}  set {stname}=%s,err_msg=%s where task_name=%s and task_tag=%s and rawid in (" + in_str + ")"
        else:
            sql = f"update {table} set {stname}=%s,err_msg=%s where task_name=%s and task_tag=%s and rawid in (" + in_str + ")"
        args = (code, err_msg, task_name, task_tag) + tuple(list_rawid)
        await SQLHelper.execute_commit(sql, args)

    @staticmethod
    async def save_taskinfo(taskinfo):
        """
        执行 taskinfo的sql
        :param taskinfo:
        :return:
        """
        sql = f"replace into {SQLTable.taskinfo}(task_name,task_tag,task_tag_next,order_num,url,req_type,`groups`,`funcs`," \
              "resp_middler,headers,params,marks,parse_rules,etl_rules," \
              "validate_rules,timeout,turn_page,post_data,state_key,post_data_is_json,introduction,others_parameter,proxy_state)values(%s,%s,%s,%s,%s,%s," \
              "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        args = (taskinfo["task_name"], taskinfo["task_tag"], taskinfo["task_tag_next"], taskinfo["order_num"],
                taskinfo["url"], taskinfo["req_type"],
                taskinfo["groups"], taskinfo["funcs"], taskinfo["resp_middler"], taskinfo["headers"],
                taskinfo["params"],
                taskinfo["marks"], taskinfo["parse_rules"], taskinfo["etl_rules"],
                taskinfo["validate_rules"], taskinfo["timeout"], taskinfo["turn_page"],
                taskinfo["post_data"], taskinfo["state_key"], taskinfo["post_data_is_json"],
                taskinfo["introduction"], taskinfo["others_parameter"], taskinfo["proxy_state"])
        await SQLHelper.execute_commit(sql, args)

    @staticmethod
    async def update_task(setinfo, infodict, data):
        sql = f'update taskinfo set {setinfo["key"]} = "{Mysql.escape(json.dumps(infodict, ensure_ascii=False))}" where ' \
              f'task_name = "{data["basekey"]["task_name"]}" and task_tag = "{data["basekey"]["task_tag"]}" and order_num = {data["basekey"]["order_num"]} and `groups` = "{data["basekey"]["groups"]}"'
        await SQLHelper.execute_commit(sql)

    @staticmethod
    async def update_task2(setinfo, data):
        sql = f'update taskinfo set "{setinfo["key"]}" = "{setinfo["value"]}" where ' \
              f'task_name = "{data["basekey"]["task_name"]}" and task_tag = "{data["basekey"]["task_tag"]}" and order_num = {data["basekey"]["order_num"]} and groups = "{data["basekey"]["groups"]}"'
        await SQLHelper.execute_commit(sql)

    @staticmethod
    async def select_taskinfo(data):
        sql = f"select * from taskinfo where task_name=%s and task_tag=%s and order_num=%s and `groups`=%s "
        sql_args = (
            data['basekey']['task_name'], data['basekey']['task_tag'],
            data['basekey']['order_num'], data['basekey']['groups'])
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    # @staticmethod
    # async def update_taskinfo(sql):
    #     await SQLHelper.execute_commit(sql)

    @staticmethod
    async def save_taskinfo_set(taskset):
        sql = f"replace into {SQLTable.taskinfo_save_setting}(task_name,task_tag,id_list,nosql_table,history_cnt, task_state,tag_state,table_name,final_stat, proxy_ctrl, proxy_type, introduction, api_url, model_name, sql_exc, sub_db_id, table_latest,table_index, force_index,sql_model_name,dispatch_model_name)values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        args = (taskset["task_name"], taskset["task_tag"], taskset["id_list"], taskset["nosql_table"],
                taskset["history_cnt"], taskset["task_state"], taskset["tag_state"], taskset["table_name"],
                taskset["final_stat"], taskset["proxy_ctrl"], taskset["proxy_type"], taskset["introduction"],
                taskset["api_url"], taskset["model_name"], taskset["sql_exc"], taskset["sub_db_id"],
                taskset["table_latest"], taskset["table_index"], taskset["force_index"], taskset["sql_model_name"],
                taskset["dispatch_model_name"])
        await SQLHelper.execute_commit(sql, args)

    # 不在写入sql 202305029
    # @staticmethod
    # async def insert_errinfo(task_name, task_tag, Id, batch, err_msg, tag):
    #     sql = f"insert ignore into {SQLTable.err_except_data}(err_type, task_name, task_tag, batch, err_str, type_name, state) values(%s, %s, %s, %s, %s, %s, 0)"
    #     err_type = "_".join([task_name, task_tag, Id])
    #     args = (err_type, task_name, task_tag, batch, err_msg, f"server_{tag}_failed")
    #     await SQLHelper.execute_commit(sql, args)

    @staticmethod
    async def update_parse_wrong_info(table, last_state, where):
        """不在使用
        """
        task_list = list()
        for k, v in where.items():
            data = "%s = '%s'" % (k, Mysql.escape(str(v)))
            task_list.append(data)
        last_str = " and ".join(task_list)
        sql = f"update {table} set {last_state} = 909 where " + last_str
        await SQLHelper.execute_commit(sql)

    ##################################### 以上为基础操作################################

    # @staticmethod
    # def set_state(pm):
    #     """
    #     生成多叉树解决打标问题
    #     """
    #     lists = []
    #     # 取出同一个 taskname 和 tasktag 的所有配置
    #     if not hasattr(pm, "redis_model"):
    #         raise Exception("pm 不存在redis_model")
    #     # keys为1_1 即 数据库taskinfo中的groups_ordernum
    #     for keys in pm.redis_model.parse_dict.keys():
    #         one_para: TaskAllModel = pm.redis_model.parse_dict[keys]
    #         # taskinfo表中的 state_key 转换成dicts
    #         state_dicts = pm.get_state_key(type="eval")
    #
    #         dicts = {
    #             # last_key 代表一条数据的最终标识(一条数据可能有多个标识位)
    #             "g_key": state_dicts["last_key"],
    #             # askinfo表中 state_key 的字典
    #             "state_dicts": state_dicts,
    #             # keys为1_1 即 数据库taskinfo中的groups_ordernum
    #             "keys": keys
    #         }
    #         lists.append(dicts)
    #     # 按照最终标识排序
    #     lists.sort(key=itemgetter('g_key'))
    #     # 按照最终标识分组
    #     lstg = groupby(lists, itemgetter('g_key'))
    #     # 顶节点 没有实际意义
    #     top_node = StateNode(node_type="top")
    #     # 会得到每组的g_key和这个组的list
    #     for g_key, group in lstg:
    #         # 生成顶节点的子节点，想想n叉树结构，顶节点只是个头，没有意义，这里的is_true标识着
    #         # 这是一个最终标识的节点
    #         is_true_node = StateNode("is_true", g_key, list(group))
    #         # 顶节点下一层是最终状态节点
    #         top_node.add_child(is_true_node)
    #     # 循环最终标识节点
    #     for is_true_node in top_node.get_child():
    #         lists = []
    #         # 获取最终状态节点的信息
    #         for item in is_true_node.get_l_d_info():
    #             # 获取s_key，标识单个taskinfo配置的状态
    #             item["g_key"] = item["state_dicts"]["s_key"]
    #             lists.append(item)
    #         # 按照s_key排序
    #         lists.sort(key=itemgetter('g_key'))
    #         # 按照s_key 分组
    #         lstg = groupby(lists, itemgetter('g_key'))
    #
    #         for g_key, group in lstg:
    #             # state是最小子节点
    #             state_node = StateNode("state", g_key, list(group))
    #             # 顶节点下一层是最终状态节点
    #             is_true_node.add_child(state_node)
    #
    #     top_node.get_rsult(pm)

    # ############################## 以下 paper
    @staticmethod
    async def get_paper_start(task_name, task_tag, year, months):
        sql = f"select * from {SQLTable.paper_days} where task_name=%s and task_tag=%s and year=%s and months =%s"
        sql_args = (task_name, task_tag, year, months)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_paper_days(task_name, task_tag, year, months, days):
        sql = f"select * from {SQLTable.paper_days} where task_name=%s and task_tag=%s and year=%s and months =%s and days =%s"
        sql_args = (task_name, task_tag, year, months, days)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_paper_page(task_name, task_tag, rawid_page):
        sql = f"select * from {SQLTable.paper_page} where task_name=%s and task_tag=%s and and rawid_page=%s"
        sql_args = (task_name, task_tag, rawid_page)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_paper_article(task_name, task_tag, rawid):
        sql = f"select * from {SQLTable.paper_article} where task_name=%s and task_tag=%s and rawid=%s"
        sql_args = (task_name, task_tag, rawid)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def check_sub_sb(sub_db_id, sub_db, product, provider):
        sql = f"select count(*) as cnt from {SQLTable.sub_db} where sub_db_id=%s and sub_db=%s and product=%s and provider=%s"
        sql_args = (sub_db_id, sub_db, product, provider)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    ############################## 百科
    @staticmethod
    async def get_baike_home(task_name, task_tag, home_rawid):
        sql = f"select * from {SQLTable.baike_home} where task_name=%s and task_tag=%s and home_rawid=%s"
        sql_args = (task_name, task_tag, home_rawid)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_baike_article(task_name, task_tag, rawid):
        sql = f"select * from {SQLTable.baike_article} where task_name=%s and task_tag=%s and rawid=%s"
        sql_args = (task_name, task_tag, rawid)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    ############################## 政策
    @staticmethod
    async def get_policy_list(task_name, task_tag, list_rawid, page_index):
        sql = f"select * from {SQLTable.policy_list} where task_name=%s and task_tag=%s and list_rawid=%s and page_index=%s"
        sql_args = (task_name, task_tag, list_rawid, page_index)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_policy_article(task_name, task_tag, rawid):
        sql = f"select * from {SQLTable.policy_article} where task_name=%s and task_tag=%s and rawid=%s"
        sql_args = (task_name, task_tag, rawid)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_nssd_list(task_name, task_tag, list_rawid, page_index):
        sql = f"select * from {SQLTable.nssd_list} where task_name=%s and task_tag=%s and list_rawid=%s and page_index=%s"
        sql_args = (task_name, task_tag, list_rawid, page_index)
        row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_otherlist(task_name, task_tag, list_rawid, page_index):
        sql = f"select * from {SQLTable.other_list} where task_name=%s and task_tag=%s and list_rawid=%s and page_index=%s"
        args = (task_name, task_tag, list_rawid, page_index)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_subclasslist(task_name, task_tag, list_rawid, page_index):
        sql = f"select * from {SQLTable.other_sub_class} where task_name=%s and task_tag=%s and list_rawid=%s and page_index=%s"
        args = (task_name, task_tag, list_rawid, page_index)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_otherarticle(task_name, task_tag, rawid):
        sql = f"select * from {SQLTable.other_article} where task_name=%s and task_tag=%s and rawid=%s"
        args = (task_name, task_tag, rawid)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    @staticmethod
    async def get_otherarticle_spe(task_name, task_tag, rawid):
        sql = f"select * from {SQLTable.other_article_spe} where task_name=%s and task_tag=%s and rawid=%s"
        args = (task_name, task_tag, rawid)
        row = await SQLHelper.fetchone(sql, args, aiomysql.DictCursor)
        return row

    # ############################ Medjournals 中华医学期刊网
    # @staticmethod
    # async def get_medjournals_years(task_name, task_tag, years_rawid, years):
    #     sql = f"select * from {SQLTable.medjournals_years} where task_name=%s and task_tag=%s and years_rawid=%s and years=%s"
    #     sql_args = (task_name, task_tag, years_rawid, years)
    #     row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
    #     return row
    #
    # @staticmethod
    # async def get_medjournals_issue(task_name, task_tag, journal_rawid, pub_year, num):
    #     sql = f"select * from {SQLTable.medjournals_issue} where task_name=%s and task_tag=%s and journal_rawid=%s and pub_year=%s and num=%s"
    #     sql_args = (task_name, task_tag, journal_rawid, pub_year, num)
    #     row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
    #     return row
    #
    # @staticmethod
    # async def get_medjournals_article(task_name, task_tag, rawid):
    #     sql = f"select * from {SQLTable.medjournals_article} where task_name=%s and task_tag=%s and rawid=%s"
    #     sql_args = (task_name, task_tag, rawid)
    #     row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
    #     return row

    @staticmethod
    async def execute_commit_sql(sql, value_list):
        row = await SQLHelper.execute_commit(sql, value_list)
        return row

    @staticmethod
    async def fetchall_query(sql, lists, field, table_name, where, order, limit):
        sql = f" SELECT {field} from {table_name} where {where} order by {order} limit {limit}"
        row = await SQLHelper.fetchall(sql, lists, aiomysql.DictCursor)
        return row

    @staticmethod
    async def delete(lists, table_name, where):
        sql = f"delete from {table_name} where {where}"
        await SQLHelper.execute_commit(sql, lists)
        return sql
